package com.aiit.controller;

import com.aiit.mapper.UserMapper;
import com.aiit.pojo.Department;
import com.aiit.pojo.Post;
import com.aiit.pojo.User;
import com.aiit.service.PostService;
import com.aiit.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: LuTao
 * @Date: 2022/04/29/22:32
 */
@Controller
public class PostController {
    @Autowired
    private UserMapper userMapper;
      @Autowired
      private UserService userService;
      @Autowired
      private PostService postService;

     @GetMapping("post_table")
       public String post_table(Model model){
//         List<User> list02 = userService.list();
//         for (User user : list02) {
//             Integer id = user.getId();
//             Post postByUserId = postService.findPostByUserId(id);
//             model.addAttribute("post",postByUserId);
//             System.out.println(postByUserId);
//         }
//         QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//          queryWrapper.select("id");
//         User user = userMapper.selectOne(queryWrapper);
//         Integer id = user.getId();
//         postService.findPostByUserId(id);
         List<Post> posts = postService.PostAll();
         model.addAttribute("post",posts);
         return "/posttable/post_table";
    }
    @GetMapping("post/delete/{uid}")
    public String deleteDepartment(@PathVariable("uid") Integer uid,
                                   @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                                   RedirectAttributes ra){
        ra.addAttribute("pn",pn);
        postService.removeById(uid);
        return "redirect:/post_table";
    }
    @RequestMapping(value = "/postadd",method = RequestMethod.POST)
    public String postadd(Post post){
        postService.posts(post);
        return "redirect:/post_table";
    }
}
